class EmailAddressController {
    
    def index = { redirect(action:create,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def delete = {
        def emailAddressInstance = EmailAddress.get( params.id )
        if(emailAddressInstance) {
            def userId = emailAddressInstance.user.id
            def mailAddress = emailAddressInstance.toString()
            emailAddressInstance.delete()
            flash.message = "EmailAddress ${mailAddress} deleted"
            redirect(controller: 'user', action:'show', id: userId )
        }
        else {
            flash.message = "EmailAddress not found with id ${params.id}"
            redirect(controller: 'user', action:'list')
        }
    }

    def edit = {
        def emailAddressInstance = EmailAddress.get( params.id )

        if(!emailAddressInstance) {
            flash.message = "EmailAddress not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ emailAddressInstance : emailAddressInstance ]
        }
    }

    def setAsMain = {
      def emailAddressInstance = EmailAddress.read( params.id )
      if(emailAddressInstance) {
        EmailAddress.executeUpdate("update EmailAddress e set e.isMain = true where e.id = ${emailAddressInstance.id}")
        EmailAddress.executeUpdate("update EmailAddress e set e.isMain = false where e.id != ${emailAddressInstance.id} and e.user.id = ${emailAddressInstance.user.id}")
        redirect(controller: 'user', action:'show', id: emailAddressInstance.user.id )
      } else {
        flash.message = "EmailAddress not found with id ${params.id}"
        redirect(controller: 'user', action:'list')
      }
    }

    def update = {
        def emailAddressInstance = EmailAddress.get( params.id )
        if(emailAddressInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(emailAddressInstance.version > version) {
                    
                    emailAddressInstance.errors.rejectValue("version", "emailAddress.optimistic.locking.failure", "Another user has updated this EmailAddress while you were editing.")
                    render(view:'edit',model:[emailAddressInstance:emailAddressInstance])
                    return
                }
            }
            emailAddressInstance.properties = params
            emailAddressInstance.email = emailAddressInstance.email.toLowerCase()
            if(!emailAddressInstance.hasErrors() && emailAddressInstance.save()) {
                flash.message = "EmailAddress ${emailAddressInstance} updated"
                redirect(controller:'user', action:'show',id:emailAddressInstance.user.id)
            }
            else {
                render(view:'edit',model:[emailAddressInstance:emailAddressInstance])
            }
        }
        else {
            flash.message = "EmailAddress not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def emailAddressInstance = new EmailAddress()
        emailAddressInstance.properties = params
        return ['emailAddressInstance':emailAddressInstance]
    }

    def save = {
        def emailAddressInstance = new EmailAddress(params)
        emailAddressInstance.email = emailAddressInstance.email.toLowerCase()
        if(!emailAddressInstance.hasErrors() && emailAddressInstance.save()) {
            flash.message = "EmailAddress ${emailAddressInstance} created"
            redirect(controller:'user', action:'show',id:emailAddressInstance.user.id)
        }
        else {
            render(view:'create',model:[emailAddressInstance:emailAddressInstance])
        }
    }
}
